package com.tarena.lbs.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tarena.lbs.base.common.utils.Asserts;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.common.principle.UserPrinciple;
import com.tarena.lbs.common.utils.AuthenticationContextUtils;
import com.tarena.lbs.user.dao.mapper.UserGroupMapper;
import com.tarena.lbs.user.dao.repository.AdminRepository;
import com.tarena.lbs.user.dao.repository.UserGroupRepository;
import com.tarena.lbs.user.pojo.param.UserGroupParam;
import com.tarena.lbs.user.pojo.po.AdminPO;
import com.tarena.lbs.user.pojo.po.UserGroupPO;
import com.tarena.lbs.user.pojo.query.UserGroupQuery;
import com.tarena.lbs.user.pojo.vo.UserGroupVO;
import com.tarena.lbs.user.service.UserGroupService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UserGroupServiceImpl extends ServiceImpl<UserGroupMapper, UserGroupPO> implements UserGroupService {
    @Autowired
    private UserGroupRepository userGroupRepository;

    @Override
    public Integer saveUserGroup(UserGroupParam param) throws BusinessException{
        Integer  adminId=getUserId();

        log.info("adminId的值:{}",adminId);
        Integer businessId = checkAndGetBusinessId(adminId);
        //4 得到商家id
        UserGroupPO  userGroupPO = new UserGroupPO();
        BeanUtils.copyProperties(param,userGroupPO);
        userGroupPO.setBusinessId(businessId);
        userGroupPO.setCreateAt(new java.util.Date());
        log.debug("保存人群数据{}",userGroupPO);
        Integer result = userGroupRepository.saveUserGroup(userGroupPO);
        return result;
    }
    @Override
    public PageResult<UserGroupVO> pageList(UserGroupQuery query) throws BusinessException {
        //1.获取用户认证对象 用户认证解析失败
        Integer adminId=getUserId();
        log.debug("adminId的值:{}",adminId);
        //2.查询adminPO 获取businessId 没有商家
       Integer businessId =  checkAndGetBusinessId(adminId);
        //3.获取当前商家的用户人群列表
        List<UserGroupVO> vos=userGroupRepository.rgetUserGroupsByBizId(businessId);
        //4.封装分页返回
        PageResult<UserGroupVO> voPage=assemblePage(vos);
        log.debug("用户人群 分页查询结果:{}",voPage);
        return voPage;
    }


    @Autowired
    private AdminRepository adminRepository;
    private Integer checkAndGetBusinessId(Integer adminId) throws BusinessException {
        AdminPO adminPO = adminRepository.getById(adminId);
        Asserts.isTrue(adminPO==null,new BusinessException("-2","后台账号不存在"));
        return adminPO.getBusinessId();
    }

    private Integer getUserId() throws BusinessException {
        UserPrinciple userPrinciple = AuthenticationContextUtils.get();
        Asserts.isTrue(userPrinciple==null,new BusinessException("-2","认证解析失败"));
        return userPrinciple.getId();
    }

    private PageResult<UserGroupVO> assemblePage(List<UserGroupVO> vos) {
        PageResult<UserGroupVO> voPage=new PageResult<>(10,1);
        voPage.setTotal(100L);//设置总共需要查出的记录数
        if (vos!=null&&vos.size()>0) {
            log.debug("命中的用户人群不为空,值:{}", vos);
        }
        voPage.setObjects(vos);
        return voPage;
    }
}
